package com.apexsoft.monitor.common.CacheLoad;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.SpringContextUtil;
import com.apexsoft.monitor.common.MonitorConstant;
import com.apexsoft.monitor.common.http.HttpHelper;
import com.apexsoft.monitor.front.console.app.om.AppNodeNew;
import com.apexsoft.monitor.front.console.app.service.AppNodeService;
import com.apexsoft.monitor.front.console.host.service.HostService;
import com.google.common.cache.CacheLoader;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MonitorCacheLoad extends CacheLoader<String,Object>{

    @Override
    public Object load(String key) throws Exception {
        HostService hostService = (HostService)SpringContextUtil.getBean("hostService");
        AppNodeService appNodeService = (AppNodeService)SpringContextUtil.getBean("appNodeService");
        JSONObject result = new JSONObject();

        switch (key){
            case MonitorConstant.CACHE_KEY_HOST:
                result = hostService.getAllHost();
                return result;
            case MonitorConstant.CACHE_KEY_NODE:
                //将url地址存入list中进行遍历
                List<String> urlList = new ArrayList<>();
                JSONObject hostRest = hostService.getAllHostByStatus();
                if(hostRest.getIntValue(Result.CODE)>0){
                    JSONArray hostList = hostRest.getJSONArray(Result.ROWS);
                    for (int i = 0; i <hostList.size(); i++) {
                        JSONObject hostJson = hostList.getJSONObject(i);

                        String hostIP = hostJson.getString("hostIp");
                        String agentPort = hostJson.getString("agentPort");
                        if (StringUtils.isNotBlank(hostIP)&&StringUtils.isNotBlank(agentPort)){
                            String url= MonitorConstant.HTTP_Front + hostIP+":"+agentPort+"+/ctrl";
                            urlList.add(url);
                        }
                    }

                    //请求头
                    Map<String,String> headerMap = new HashMap<>();
                    headerMap.put("HeaderToken","81CF7DDC68CDF393D3FC705050F71029");
                    //入参
                    JSONObject paramsJson = new JSONObject();
                    paramsJson.put("func","Node_List");


                    List<AppNodeNew> nodeList = new ArrayList<>();
                    for (String urlLi : urlList){
                        //调用工具类HttpHelper获取列表数据
                        for(int i=0;i<3;i++){
                            String returnValue =(String) HttpHelper.post(urlLi,paramsJson.toJSONString(),headerMap);
                            JSONObject resultTemp = JSONObject.parseObject(returnValue);
                            if(resultTemp.getIntValue(Result.CODE)>0){
                                List<AppNodeNew> list = new ArrayList<>();
                                JSONArray nodeDatas = resultTemp.getJSONArray(Result.RECORDS);
                                for(int j=0;j<nodeDatas.size();j++){
                                    JSONObject nodeJson = nodeDatas.getJSONObject(j);
                                    AppNodeNew nodeObject = new AppNodeNew(nodeJson);
                                    list.add(nodeObject);
                                }
                                nodeList.addAll(list);
                                break;
                            }else{
                                Thread.sleep(500);
                            }
                        }
                    }
                    result = Result.commonListResult(nodeList);
                }else{
                    result.put(Result.CODE, Result.EC_1000);
                    result.put(Result.NOTE, "暂无主机列表");
                }
                return result;
            default:
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "key 值不存在");
                return result;
        }
    }
}
